from flask import Flask, render_template, request
import pandas as pd
import matplotlib.pyplot as plt
import plotly as py
import plotly.graph_objs as go
import cufflinks as cf

app = Flask(__name__)


df = pd.read_csv('hurun.csv', encoding='utf-8', delimiter="\t")
cf.set_config_file(offline=True, theme="ggplot")
py.offline.init_notebook_mode()
pyplt = py.offline.plot
环杭州湾大湾区_data = df.query("region=='环杭州湾大湾区'")
渤海大湾区_data = df.query("region=='渤海大湾区'")
粤港澳大湾区_data = df.query("region=='粤港澳大湾区'")


@app.route('/')
def shouye():
	return render_template('shouye.html')


@app.route('/huanhangzhouwan')
def huanhangzhouwan():
	环杭州湾大湾区_data = df.query("region=='环杭州湾大湾区'")
	环杭州湾大湾区_data_str = df.query("region=='环杭州湾大湾区'").to_html()
	sum_环杭州湾大湾区各行业估值 = 环杭州湾大湾区_data.groupby("行业").agg({"企业名称":"count","估值（亿人民币）":"sum"}).sort_values(by = "企业名称",ascending = False )
	# tu = plt.bar(sum_环杭州湾大湾区各行业估值.index,sum_环杭州湾大湾区各行业估值["估值（亿人民币）"])
	fig = 环杭州湾大湾区_data.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True)
	py.offline.plot(fig, filename="example.html", auto_open=False)
	with open("example.html", encoding="utf8", mode="r") as f:
		plot_all = "".join(f.readlines())
	
	return render_template('huanhangzhouwan.html',the_plot = plot_all,data = 环杭州湾大湾区_data_str,)


@app.route('/bohai')
def bohai():
	渤海大湾区_data = df.query("region=='渤海大湾区'")
	渤海大湾区_data_str = df.query("region=='渤海大湾区'").to_html()
	sum_渤海大湾区各行业估值 = 渤海大湾区_data.groupby("行业").agg({"企业名称":"count","估值（亿人民币）":"sum"}).sort_values(by = "企业名称",ascending = False )
	fig = 渤海大湾区_data.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True)
	py.offline.plot(fig, filename="example.html", auto_open=False)
	with open("example.html", encoding="utf8", mode="r") as f:
		plot_all = "".join(f.readlines())

	return render_template('bohai.html',the_plot = plot_all,data = 渤海大湾区_data_str)


@app.route('/yuegangao')
def yuegangao():
	粤港澳大湾区_data = df.query("region=='粤港澳大湾区'")
	粤港澳大湾区_data_str = df.query("region=='粤港澳大湾区'").to_html()
	sum_粤港澳大湾区各行业估值 = 粤港澳大湾区_data.groupby("行业").agg({"企业名称":"count","估值（亿人民币）":"sum"}).sort_values(by = "企业名称",ascending = False )
	fig = 粤港澳大湾区_data.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True)
	py.offline.plot(fig, filename="example.html", auto_open=False)
	with open("example.html", encoding="utf8", mode="r") as f:
		plot_all = "".join(f.readlines())

	return render_template('yuegangao.html',the_plot = plot_all,data = 粤港澳大湾区_data_str)


@app.route('/sangewanqu')
def sangewanqu():
	三个大湾区_data = df
	三个大湾区_data_str = df.to_html()
	sum_三个湾区各行业估值对比 = df.groupby(['行业','region']).agg({"估值（亿人民币）":"sum"})
	reset_index = sum_三个湾区各行业估值对比.reset_index("region")
	hangzhou_qu = reset_index[reset_index["region"] == "环杭州湾大湾区"]
	hangzhou_guzhi = hangzhou_qu["估值（亿人民币）"]
	bohai_qu = reset_index[reset_index["region"] == "渤海大湾区"]
	bohai_guzhi = bohai_qu["估值（亿人民币）"]
	yuegangao_qu = reset_index[reset_index["region"] == "粤港澳大湾区"]
	yuegangao_guzhi = yuegangao_qu["估值（亿人民币）"]
	list_行业 = ["云计算","人工智能","健康科技","共享经济","区块链","大数据","媒体和娱乐","房地产科技","教育科技","新能源","新能源汽车","新零售","机器人","消费品","游戏","物流","生命科学","电子商务","网络安全","软件与服务","金融科技"]
	trace_1 = go.Bar(x = list_行业,y = hangzhou_guzhi,name = '环杭州湾大湾区')
	trace_2 = go.Bar(x = list_行业,y = bohai_guzhi,name = '渤海大湾区')
	trace_3 = go.Bar(x = list_行业,y = yuegangao_guzhi,name = '粤港澳大湾区')
	trace = [trace_1, trace_2, trace_3]
	layout = go.Layout(
            title = '三个湾区各行业估值对比图')
	figure = go.Figure(data = trace, layout = layout)
	py.offline.plot(figure, filename="example.html", auto_open=False)
	with open("example.html", encoding="utf8", mode="r") as f:
		plot_all = "".join(f.readlines())

	return render_template('sangewanqu.html',the_plot = plot_all,data = 三个大湾区_data_str)




if __name__ == '__main__':
    app.run(debug=True)
